package weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.R;

/**
 * ltx on 2017/2/24 11:29
 * android 流式布局
 */
public class AutoLineFeedLayout extends ViewGroup implements View.OnClickListener {

    private static final boolean DEBUG = true;
    private static final String TAG = "AutoLineFeedLayout";

    private int paddingLeft = 0;
    private int paddingRight = 0;
    private int paddingTop = 0;
    private int paddingBottom = 0;
    private int verticalSpacing = 0;
    private int horizontalSpacing = 0;

    private int mWidth;

    public OnItemClick mOnItemClick;

    /**
     * 设置监听入口
     * @param onItemClick onItemClick
     */
    public void setOnItemClick(OnItemClick onItemClick) {
        mOnItemClick = onItemClick;
    }

    public AutoLineFeedLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }
    public AutoLineFeedLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public AutoLineFeedLayout(Context context) {
        this(context, null);
    }

    /**
     * 获取布局文件中的一些值
     * @param attrs
     */
    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
                R.styleable.AutoLineFeedLayout);
        horizontalSpacing = (int) typedArray.getDimension(
                R.styleable.AutoLineFeedLayout_horizontalSpacing, 0);
        paddingLeft = (int) typedArray.getDimension(
                R.styleable.AutoLineFeedLayout_paddingLeft, 0);
        paddingTop = (int) typedArray.getDimension(
                R.styleable.AutoLineFeedLayout_paddingTop, 0);
        paddingBottom = (int) typedArray.getDimension(
                R.styleable.AutoLineFeedLayout_paddingBottom, 0);
        verticalSpacing = (int) typedArray.getDimension(
                R.styleable.AutoLineFeedLayout_verticalSpacing, 0);
        paddingRight = (int) typedArray.getDimension(
                R.styleable.AutoLineFeedLayout_paddingRight, 0);
        typedArray.recycle();
    }

    private void log(String string) {
        if (DEBUG) {
            Log.i(TAG, string);
        }
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        log("l=" + l + " t=" + t + " r=" + r + " b=" + b);
        int count = getChildCount();
        mWidth = getWidth();
        log("mWidth=" + mWidth);
        //第一个子View开始放置的距离父控件左边的距离
        int startOffsetX = paddingLeft;
        //第一个子View开始放置的距离父控件上边的距离
        int startOffsetY=0+paddingTop;
        int preEndsetX;
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            childView.measure(0,0);
            //子View的宽度
            int w = childView.getMeasuredWidth();
            //子View的高度
            int h = childView.getMeasuredHeight();
            log("w=" + w + "  h=" + h);
            //子View的右端距离父控件的左边的距离
            preEndsetX=startOffsetX+w;
            //如果距离大于宽度-右间距的距离，则该子View应该在新的一行
            if (preEndsetX > mWidth - paddingRight) {
                if (startOffsetX > paddingLeft) {
                    //设置新起一行的子View到父控件的距离
                    startOffsetX=paddingLeft;
                    startOffsetY = h + startOffsetY +horizontalSpacing;
                }
            }
            childView.layout(startOffsetX, startOffsetY, startOffsetX+ w, startOffsetY + h);
            //设置下一个子view开始放置的距离父控件左边的距离
            startOffsetX=startOffsetX+w+verticalSpacing;
            //设置监听
            childView.setTag(i);
            childView.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int pos= (int) v.getTag();
        if(null!=mOnItemClick){
            mOnItemClick.onSingleClick(v,pos);
        }
    }

    /**
     * 流式布局 onItemClick 接口
     */

    public interface OnItemClick{
        void onSingleClick(View view,int pos);
    }
}
